#include "configmgr.h"
#include <QJsonObject>
#include "QJsonDocument"
#include <QFile>
#include <QDebug>
#include <QDir>

QString configpath = "/home/alang/wechat/client/wechat/config.json";

ConfigMgr::ConfigMgr()
{
    //步骤1：读取文件内容
    //步骤2：转换为json格式
    //步骤3：提取对应Key和Value

    QFile file(configpath);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            qWarning() << "Failed to open file:" << configpath;
            return;
        }

        QByteArray fileData = file.readAll();
        QJsonParseError jsonParseError;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(fileData, &jsonParseError);

        if (jsonParseError.error != QJsonParseError::NoError || !jsonDoc.isObject()) {
            qWarning() << "Failed to parse JSON:" << jsonParseError.errorString();
            return;
        }

        QJsonObject root = jsonDoc.object();
        try {
            QJsonValue GateServer = root["GateServer"];
            QJsonValue Post = root["Post"];
            GateServer_Port_ = GateServer["Port"].toString();
            GateServer_Ip_ = GateServer["Ip"].toString();
            Post_GetVerifyCode_ = Post["GetVerifyCode"].toString();
            Post_UserRegister_ = Post["UserRegister"].toString();
            Post_ResetPassWord_ = Post["ResetPassWord"].toString();
            Post_Login_ = Post["Login"].toString();
        } catch (const std::invalid_argument& e) {
            qWarning() << "Invalid argument error:" << e.what();
        } catch (const std::out_of_range& e) {
            qWarning() << "Out of range error:" << e.what();
        } catch (...) {
            qWarning() << "Unknown error occurred during configuration loading";
        }
}

void ConfigMgr::PrintConfigure()const
{
    qDebug() << "*********************************Configure start****************************";
    qDebug() << "Gate Server Port:" << GateServer_Port_;
    qDebug() << "Gate Server Ip:" << GateServer_Ip_;
    qDebug() << "Post Get VerifyCode:" << Post_GetVerifyCode_;
    qDebug() << "Post User Register:" << Post_UserRegister_;
    qDebug() << "Post Reset Password:" << Post_ResetPassWord_;
    qDebug() << "Post Login:" << Post_Login_;
    qDebug() << "*********************************Csonfigure end****************************";
}
